package tools;

import Entity.orderBean;
import control.orderControl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

@WebServlet("/readOrderServlet")
public class readOrderServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
    //使用该servlet必须传type的值过来
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String order_id = request.getParameter("order_id");
        String key = request.getParameter("key");
        Integer type = Integer.valueOf(new String(request.getParameter("type").getBytes("ISO-8859-1"),"UTF-8"));
        String name = request.getParameter("name");
        //清除原先orderlist的seesion
        System.out.println(type);
        HttpSession session=request.getSession();
        session.removeAttribute("orderlist");
        //获取订单列表
        orderControl.get_orderlist(request,response,type);
        if(name==null) {
            System.out.println("获取当前订单的信息");
            try {
                ArrayList orderlist = null;
                if ((ArrayList) session.getAttribute("orderlist") != null) {
                    orderlist = (ArrayList) session.getAttribute("orderlist");
                    orderBean ff = new orderBean();
                    Iterator iter = orderlist.iterator();
                    while (iter.hasNext()) {
                        ff = (orderBean) iter.next();
                        if (ff.getOrder_id().equals(order_id)) {
                            session.setAttribute("orderBean", ff);
                            break;
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            //判断转到哪个界面
            if (key.equals("0"))
                response.sendRedirect("http://localhost:8080/order_alter.jsp");
            else
                response.sendRedirect("http://localhost:8080/order_content.jsp");
        }
        else
        {
            name = new String(name.getBytes("ISO-8859-1"),"UTF-8");
            System.out.println("检索订单"+name);

            ArrayList orderlist = (ArrayList) session.getAttribute("orderlist");
            System.out.println(orderlist);
            //http://localhost:8080/goodmanageServlet
            ArrayList orderlist2 = new ArrayList();
            orderBean ff = new orderBean();
            Iterator iter = orderlist.iterator();
            while (iter.hasNext()) {
                ff = (orderBean) iter.next();
                if (ff.getOrder_id().contains(name) || ff.getName().contains(name) || ff.getUse_id().contains(name)) {
                    orderlist2.add(ff);
                }
            }
            session.removeAttribute("orderlist");
            session.setAttribute("orderlist",orderlist2);
            response.sendRedirect("http://localhost:8080/order_manage.jsp");
        }
        return;
    }
}
